/**
 * File		: Shape.java
 * Subject	: Represents a shape
 * Author	: Isaac Parenteau (locus2k@gmail.com)
 * Date		: April 16th, 2011
 * Note 	: Code was borrowed from Google and modified to fit the projects use
 * Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package src.project.cube;

import java.nio.ShortBuffer;
import java.util.ArrayList;

import src.project.util.Color;
import src.project.util.Matrix;
import src.project.util.Vertex;

public class Shape {
	
	public static final String name = Shape.class.getSimpleName();
	
	protected ArrayList<Face> faceList = new ArrayList<Face>();
	protected ArrayList<Vertex> vertexList = new ArrayList<Vertex>();
	protected ArrayList<Integer> indexList = new ArrayList<Integer>();
	
	protected World world;
	
	protected Matrix transform;
	protected Matrix animateTransform;
	
	public int FaceIndex = 0;
	
	/**
	 * Constructor
	 * @param world world to draw the shape to
	 */
	public Shape(World world){
		this.world = world;
	}
	
	/**
	 * Adds a face to the shape
	 * @param face face to add
	 */
	public void addFace(Face face){
		faceList.add(face);
	}
	
	/**
	 * Sets a specific face to a color
	 * @param face face to set color 
	 * @param color color of the face
	 */
	public void setFaceColor(int face, Color color){
		FaceIndex = face;
		faceList.get(face).setColor(color);
	}
	
	/**
	 * Puts the indices of the face into the buffer
	 * @param buffer index buffer
	 */
	public void putIndices(ShortBuffer buffer){
		
		for(Face f : faceList)
			f.putIndices(buffer);
	}
	
	/**
	 * Adds a vertex to the vertex list
	 * @param x x coordinate of the vertex
	 * @param y y coordinate of the vertex
	 * @param z z coordinate of the vertex
	 * @return vertex with the new coordinates
	 */
	public Vertex addVertex(float x, float y, float z){
		
		//Check to see if there is an existing vertex
		for(Vertex vertex : vertexList)
			if(vertex.X == x && vertex.Y == y && vertex.Z == z)
				return vertex;
		
		//Create a new vertex
		Vertex vertex = world.addVertex(x, y, z);
		vertexList.add(vertex);
		return vertex;
	}
	
	/**
	 * Gets the index count
	 * @return index count
	 */
	public int getIndexCount(){
		
		int count = 0;
		
		for(Face f : faceList)
			count += f.getIndexCount();

		return count;
	}
	
	/**
	 * Gets a specific face
	 * @param i location of the face
	 * @return face
	 */
	public Face getFace(int i){
		return faceList.get(i);
	}
	
	/**
	 * Gets the number of faces for the shape
	 * @return number of faces
	 */
	public int getFaceCount(){
		return faceList.size();
	}

	/**
	 * Starts the animation for the shape
	 */
	public void startAnimation() {
		
	}

	/**
	 * ends the animation for the shape
	 */
	public void endAnimation() {
		if(transform == null)
			transform = new Matrix(animateTransform);
		else
			transform = transform.multiply(animateTransform);
		
	}

	public void animateTransform(Matrix transform) {
		animateTransform = transform;
		if(this.transform != null)
			transform = this.transform.multiply(transform);
		
		for(Vertex vertex : vertexList)
			world.transformVertex(vertex, transform);
	}
}
